package com.heima.tlias.service.impl;

import com.heima.tlias.controller.response.JobOptionVo;
import com.heima.tlias.dao.ClazzMapper;
import com.heima.tlias.dao.EmpMapper;
import com.heima.tlias.dao.StudentMapper;
import com.heima.tlias.service.ReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {

    private final EmpMapper empMapper;

    private final StudentMapper studentMapper;

    private final ClazzMapper clazzMapper;

    @Override
    public JobOptionVo queryEmpJobData() {
        //1. 调用数据访问层查询数据
        List<Map<String, Object>> data = empMapper.queryEmpJobData();
        //2. 进行数据处理和组装
        //2.1 获取职位列表
        List<String> jobNameList = data.stream().map(map -> map.get("jobName").toString()).collect(Collectors.toList());
        //2.2 获取职位数量列表
        List<Integer> dataList = data.stream().map(map -> Integer.parseInt(map.get("count").toString())).collect(Collectors.toList());

        return new JobOptionVo(jobNameList, dataList);
    }

    @Override
    public List<Map<String, Object>> queryEmpGenderData() {
        //调用数据访问层查询性别统计数据
        List<Map<String, Object>> empGenderData = empMapper.queryEmpGenderData();
        //对查询到的数据进行格式转化
        List<Map<String, Object>> mapList = empGenderData.stream().map(map -> Map.of("name", map.get("gender"), "value", map.get("count"))).collect(Collectors.toList());

        return mapList;
    }

    @Override
    public Map<String, List<Object>> studentCountData() {
        //查询班级人数
        List<Map<String, Object>> mapList = clazzMapper.queryStudentCount();
        //完成数据封装
        List<Object> clazzList = mapList.stream().map(map -> map.getOrDefault("name", "未知").toString()).collect(Collectors.toList());
        List<Object> dataList = mapList.stream().map(map -> Integer.parseInt(map.get("count").toString())).collect(Collectors.toList());

        return Map.of("clazzList", clazzList, "dataList", dataList);
    }

    @Override
    public List<Map<String, Object>> studentDegreeData() {
        List<Map<String, Object>> studentDegreeCount = studentMapper.studentDegreeCount();
        return studentDegreeCount.stream().map(map -> Map.of("name", map.get("degree"), "value", map.get("count"))).collect(Collectors.toList());
    }
}
